﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Xk
{
    public partial class frmChooseCourseResult : Form
    {
        public frmChooseCourseResult()
        {
            InitializeComponent();
        }

        private void getCourse()
        {
            SqlConnection cn = new SqlConnection(Properties.Settings.Default.XkConnectionString);
            string sql = "SELECT CouNo='',CouName='请选择课程'";
            sql += " UNION SELECT CouNo,CouName FROM Course";
            sql += " ORDER BY CouNo";
            SqlDataAdapter da = new SqlDataAdapter(sql, cn);
            DataSet ds = new DataSet();
            cn.Open();
            da.Fill(ds, "Course");
            cn.Close();
            cbCourse.ValueMember = "CouNo";
            cbCourse.DisplayMember = "CouName";
            cbCourse.DataSource = ds.Tables["Course"];
        }

        private void getStudent()
        {
            if (cbCourse.SelectedIndex > 0)
            {
                SqlConnection cn = new SqlConnection(Properties.Settings.Default.XkConnectionString);
                string sql = "SELECT * FROM Student,Class";
                sql += " WHERE StuNo IN(SELECT StuNo FROM StuCou WHERE CouNo=@CouNo AND State='选中')";
                sql += " AND Student.ClassNo=Class.ClassNo";
                sql += " ORDER BY StuNo";
                SqlDataAdapter da = new SqlDataAdapter(sql, cn);
                da.SelectCommand.Parameters.Add("CouNo", SqlDbType.NVarChar, 8).Value = cbCourse.SelectedValue.ToString();
                DataSet ds = new DataSet();
                cn.Open();
                da.Fill(ds, "Student");
                cn.Close();
                dataGridView1.DataSource = ds.Tables["Student"];
            }
        }

        private void frmChooseCourseResult_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            getCourse();
        }

        private void cbCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            getStudent();
        }
    }
}
